package com.gamex.findit.controller.api;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.gamex.findit.ResultCode;
import com.gamex.findit.internal.DefaultConfig;
import com.gamex.findit.model.ADImage;
import com.gamex.findit.service.ADImageService;
import com.gamex.findit.util.CommonUtil;

@Controller("memberADImage")
public class ADImageController {
private static final Logger logger = LoggerFactory.getLogger(ADImageController.class);
	
	@Autowired ADImageService adImageService;
	@Autowired DefaultConfig config;
	
	/**
	 * @param session
	 * @param request
	 * @param response
	 * @param model
	 * @param adImage
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/app/member/newAdImageList.*")
	public ResponseEntity<String> newAdImageList( HttpSession session
			  			   , HttpServletRequest request
						   , HttpServletResponse response
						   , ModelMap model
						   , @ModelAttribute ADImage adImage) throws Exception{
		
	HttpHeaders responseHeaders = new HttpHeaders();
    responseHeaders.add("Content-Type", "application/json; charset=utf-8");
	ModelMap body = new ModelMap();
	List<ADImage> result = null;
	
	try {
		
		result = adImageService.getAD(adImage);
		body.addAttribute(ResultCode.RESULT,ResultCode.RESULT_CODE_SUCCESS);
	} catch (Exception e) {
		body.addAttribute(ResultCode.RESULT, ResultCode.RESULT_CODE_FAIL);
	}
	
	body.addAttribute("list", result);
	
	return new ResponseEntity<String>(CommonUtil.toJson(body), responseHeaders, HttpStatus.CREATED);
	}

	/**
	 * @param session
	 * @param request
	 * @param response
	 * @param model
	 * @param adImage
	 * @throws Exception
	 */
	@RequestMapping("/app/member/hitViewCount.*")
	public ResponseEntity<String> hitViewCount( HttpSession session
				  			   , HttpServletRequest request
							   , HttpServletResponse response
							   , ModelMap model
							   , @ModelAttribute ADImage adImage) throws Exception{
		ModelMap body = new ModelMap();
		HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.add("Content-Type", "application/json; charset=utf-8");
		
		adImageService.hitViewCount(adImage.getAdId());
		body.addAttribute(ResultCode.RESULT,ResultCode.RESULT_CODE_SUCCESS);
		return new ResponseEntity<String>(CommonUtil.toJson(body), responseHeaders, HttpStatus.CREATED);

	}

	/**
	 * @param session
	 * @param request
	 * @param response
	 * @param model
	 * @param character
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/app/member/adImageView")
	public ResponseEntity<String> viewcharacter( HttpSession session
							, HttpServletRequest request
							, HttpServletResponse response
							, ModelMap model
							, @ModelAttribute ADImage adImage) throws Exception{
		
		HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.add("Content-Type", "application/json; charset=utf-8");
        
		ModelMap body = new ModelMap();
		ADImage result = null;
		
		try {
			result = adImageService.getADView(adImage);
		} catch (Exception e) {
			body.addAttribute(ResultCode.RESULT, ResultCode.RESULT_CODE_FAIL);
		}
		
		if(result != null){
			body.addAttribute(ResultCode.RESULT,ResultCode.RESULT_CODE_SUCCESS);
			body.addAttribute("detail", result);
		}else{
			body.addAttribute(ResultCode.RESULT, ResultCode.RESULT_CODE_FAIL);
		}
		
	return new ResponseEntity<String>(CommonUtil.toJson(body), responseHeaders, HttpStatus.CREATED);
	}
	
}
